<?php

if ( ! defined('HALO_BOOTSTRAPPED') ) {

    if ( ! defined('HALO_ROOT_DIR') ) {
        define('HALO_ROOT_DIR', dirname(dirname(__FILE__)));
    }

    // Used to try and discover where the Halo libs live. This can safely be removed from
    // actual projects if Halo lives in vendors/ .
    $possibleHaloDispatcherLocations = array(
        implode(
            DIRECTORY_SEPARATOR,
            array(dirname(dirname(__FILE__)), 'halo', 'halo_Dispatcher.php')
        ),
        implode(
            DIRECTORY_SEPARATOR,
            array(dirname(dirname(dirname(dirname(__FILE__)))), 'lib', 'halo_Dispatcher.php')
        ),
    );

    foreach ( $possibleHaloDispatcherLocations as $possibleHaloDispatcherLocation ) {
        if ( file_exists($possibleHaloDispatcherLocation) ) {
            hc_core_ClassLoader::appendPaths(dirname($possibleHaloDispatcherLocation));
            break;
        }
    }

    if ( ! defined('HALO_LIB_DIR') ) {
        if ( isset($_SERVER['HALO_LIB_DIR']) ) {
            define('HALO_LIB_DIR', $_SERVER['HALO_LIB_DIR']);
        } else {
            define('HALO_LIB_DIR', HALO_ROOT_DIR . DIRECTORY_SEPARATOR . 'lib');
        }
    }

    hc_core_ClassLoader::appendPaths(HALO_LIB_DIR);

    if ( ! defined('HALO_CONFIG_DIR') ) {
        if ( isset($_SERVER['HALO_CONFIG_DIR']) ) {
            define('HALO_CONFIG_DIR', $_SERVER['HALO_CONFIG_DIR']);
        } else {
            define('HALO_CONFIG_DIR', HALO_ROOT_DIR . DIRECTORY_SEPARATOR . 'config');
        }
    }

    hc_core_ClassLoader::appendPaths(HALO_CONFIG_DIR);

    if ( ! defined('HALO_VIEWS_DIR') ) {
        if ( isset($_SERVER['HALO_VIEWS_DIR']) ) {
            define('HALO_VIEWS_DIR', $_SERVER['HALO_VIEWS_DIR']);
        } else {
            define('HALO_VIEWS_DIR', HALO_ROOT_DIR . DIRECTORY_SEPARATOR . 'views');
        }
    }

    if ( ! defined('HALO_THEMES_DIR') ) {
        if ( isset($_SERVER['HALO_THEMES_DIR']) ) {
            define('HALO_THEMES_DIR', $_SERVER['HALO_THEMES_DIR']);
        } else {
            define('HALO_THEMES_DIR', HALO_ROOT_DIR . DIRECTORY_SEPARATOR . 'themes');
        }
    }

    if ( ! defined('HALO_TEMP_DIR') ) {
        if ( isset($_SERVER['HALO_TEMP_DIR']) ) {
            define('HALO_TEMP_DIR', $_SERVER['HALO_TEMP_DIR'] . DIRECTORY_SEPARATOR);
        } else {
            define('HALO_TEMP_DIR', HALO_ROOT_DIR . DIRECTORY_SEPARATOR . 'tmp');
        }
    }

    if ( ! defined('HALO_VENDORS_DIR') ) {
        if ( isset($_SERVER['HALO_VENDORS_DIR']) ) {
            define('HALO_VENDORS_DIR', $_SERVER['HALO_VENDORS_DIR'] . DIRECTORY_SEPARATOR);
        } else {
            define('HALO_VENDORS_DIR', HALO_ROOT_DIR . DIRECTORY_SEPARATOR . 'vendors');
        }
    }

    if ( is_dir(HALO_VENDORS_DIR) ) {
        if ( $dirHandle = opendir(HALO_VENDORS_DIR) ) {
            $vendorPaths = array();
            while ( ($potentialVendorDir = readdir($dirHandle)) !== false ) {
                $potentialVendorPath = HALO_VENDORS_DIR . DIRECTORY_SEPARATOR . $potentialVendorDir;
                if ( ( preg_match('/^[^\.]/', $potentialVendorDir) ) and is_dir($potentialVendorPath) ) {
                    $vendorPaths[] = $potentialVendorPath;
                }
            }
            asort($vendorPaths);
            hc_core_ClassLoader::appendPaths($vendorPaths);
        }
    }

    define('HALO_BOOTSTRAPPED', true);

}

?>
